package scoutobahn.spielwiese.xml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.xml.sax.SAXException;

import scoutobahn.highway.Highway;
import scoutobahn.highway.HighwayElement;
import scoutobahn.highway.HighwayGraph;
import scoutobahn.highway.HighwayNode;
import scoutobahn.highway.Town;
import scoutobahn.util.Stopwatch;
import scoutobahn.util.Tuple;
import scoutobahn.xml.HighwayGraphBuilder;
import scoutobahn.xml.OsmNode;
import scoutobahn.xml.XmlTownLoader;

public class MightyXmlLoader {

    public static void main(String[] args) throws XPathExpressionException, FileNotFoundException, SAXException, IOException, ParserConfigurationException {
        Stopwatch watch = new Stopwatch();
        System.out.println("loading highways");
        watch.start();
        Collection<OsmNode> townNodes = new XmlTownLoader().loadTowns(new FileInputStream("towns.xml"));
        System.out.println(townNodes.size() + " towns found.");
        HighwayGraph graph = new HighwayGraphBuilder().buildFromXml(new FileInputStream("highways.xml"), townNodes, 40000);
        for (Town town : graph.getTowns()) {
            System.out.println(town.getOsmItem().getTag("name") + 
                    " is connected to " + town.getParents().size() + " highways");
            if(town.getOsmItem().getTag("name").equals("Ulm")) {
                Collection<HighwayElement> nodes = town.getParents();
                for(HighwayElement element : nodes) {
                        Collection<HighwayElement> autobahnen = element.getParents();
                        for(HighwayElement autobahn : autobahnen) {
                            System.out.println(autobahn.getOsmItem().getTag("ref"));
                            if(autobahn.getOsmItem().getTag("ref") != null
                                    && autobahn.getOsmItem().getTag("ref").equals("A 7")) {
                                printTownsAlongHighway((Highway) autobahn, (HighwayNode) element);
                            }
                        }
                }
            }
        }
        watch.end();
        System.out.println("done.");
        long timeMilis = watch.getElapsedMilliseconds();
        int points = (int) Math.rint( (1000d/timeMilis) * 10000d );
        System.out.println("You scored " + points + " Points.");
    }
 
    private static void printTownsAlongHighway(Highway highway, HighwayNode element) {
        Tuple<HighwayNode, HighwayNode> elements = highway.getNextIntersections(element);
        while(elements.getItemOne() != null) {
            HighwayNode node = elements.getItemOne();
            for(HighwayElement parent : node.getParents()) {
                if(parent instanceof Town) {
                    System.out.println("t:" + parent.getOsmItem().getTag("name"));
                }
            }
            elements = highway.getNextIntersections(elements.getItemOne());
        }
    }
    
}
